package cn.wjh.business.controller;

import cn.wjh.business.domain.Product;
import cn.wjh.business.service.IProductService;
import cn.wjh.common.annotation.Log;
import cn.wjh.common.core.controller.BaseController;
import cn.wjh.common.core.domain.AjaxResult;
import cn.wjh.common.core.page.TableDataInfo;
import cn.wjh.common.enums.BusinessType;
import cn.wjh.common.utils.poi.ExcelUtil;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 翻译产品Controller
 *
 * @author jiraWu
 * @date 2023-11-29
 */
@RestController
@RequestMapping("/business/Product")
public class ProductController extends BaseController {
	@Resource
	private IProductService productService;

	/**
	 * 查询翻译产品列表
	 */
	@PreAuthorize("@ss.hasPermi('business:Product:list')")
	@GetMapping("/list")
	@Cacheable(value = "productListCache", keyGenerator = "cacheKeyGenerator")
//    @RateLimiter(limitType = LimitType.IP)
	public TableDataInfo list(Product product) {
		startPage();
		List<Product> list = productService.selectProductList(product);
		return getDataTable(list);
	}

	/**
	 * 获取所有产品
	 */
	@Cacheable(value = "productListCache", keyGenerator = "cacheKeyGenerator")
	@GetMapping("/selectList")
	public AjaxResult selectList() {
		List<Product> list = productService.selectList();
		return AjaxResult.success(list);
	}


	/**
	 * 导出翻译产品列表
	 */
	@PreAuthorize("@ss.hasPermi('business:Product:export')")
	@Log(title = "翻译产品", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Product product) {
		List<Product> list = productService.selectProductList(product);
		ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
		util.exportExcel(response, list, "翻译产品数据");
	}

	/**
	 * 获取翻译产品详细信息
	 */
	@PreAuthorize("@ss.hasPermi('business:Product:query')")
	@GetMapping(value = "/{productId}")
	@Cacheable(value = "productListCache", keyGenerator = "cacheKeyGenerator")
	public AjaxResult getInfo(@PathVariable("productId") Long productId) {
		return success(productService.selectProductByProductId(productId));
	}

	/**
	 * 新增翻译产品
	 */
	@PreAuthorize("@ss.hasPermi('business:Product:add')")
	@Log(title = "翻译产品", businessType = BusinessType.INSERT)
	@PostMapping
	@CacheEvict(value = "productListCache", allEntries = true)
	public AjaxResult add(@RequestBody Product product) {
		return toAjax(productService.insertProduct(product));
	}

	/**
	 * 修改翻译产品
	 */
	@PreAuthorize("@ss.hasPermi('business:Product:edit')")
	@Log(title = "翻译产品", businessType = BusinessType.UPDATE)
	@PutMapping
	@CacheEvict(value = "productListCache", allEntries = true)
	public AjaxResult edit(@RequestBody Product product) {
		return toAjax(productService.updateProduct(product));
	}

	/**
	 * 删除翻译产品
	 */
	@PreAuthorize("@ss.hasPermi('business:Product:remove')")
	@Log(title = "翻译产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
	@CacheEvict(value = "productListCache", allEntries = true)
	public AjaxResult remove(@PathVariable List<Long> productIds) {
		return toAjax(productService.deleteProductByProductIds(productIds));
	}
}
